<template>
  <div>
    <table>
      <thead>
      <tr>
        <template v-for="titleItem in value.skuAllPropTypeList">
          <th
            v-if="(!titleItem.isDefaultSkuField || value.skuList.length > 1)"
            :key="titleItem.title"
            style="white-space:nowrap"
            :style="titleItem.fieldType?'width:100px':''"
          >
            <i v-if="titleItem.required && !inputDisabled" class="required"></i>{{ titleItem.title }}
          </th>
        </template>
      </tr>
      </thead>
      <tbody>
      <tr v-for="(skuProps,skuPropIndex) in value.skuList" :key="skuPropIndex">
        <template v-for="(sku,skuIndex) in skuProps">
          <td
            v-if="sku.rowSpan > 0 && (!sku.isDefaultSkuField || value.skuList.length > 1)"
            :key="skuIndex"
            :rowspan="sku.rowSpan"
          >
            <input v-if="sku.isTextField" v-model="sku.value" type="text" :disabled="inputDisabled">
            <template v-else-if="sku.isIntegerField || sku.isIntegerIncAndDecField">
              <div v-if="sku.isIntegerIncAndDecField" class="stock-btns" style="margin-right:0">
                <div class="stock-btn" :class="{'stock-btn--active':sku.addFlag===1}" @click="sku.addFlag=1">增</div>
                <div class="stock-btn" :class="{'stock-btn--active':sku.addFlag===0}" @click="sku.addFlag=0">减</div>
              </div>
              <input
                :style="[sku.isIntegerIncAndDecField?{paddingLeft:'60px'}:null]"
                v-model="sku.value"
                :disabled="inputDisabled"
                type="text"
                @keydown="SkuUtil.handleLimitInteger"
              >
            </template>
            <input
              v-else-if="sku.isFloatField"
              v-model="sku.value"
              type="text"
              :disabled="inputDisabled"
              @blur="SkuUtil.handleLimitFloatPrecision($event,sku.precision || 2)"
              @keydown="SkuUtil.handleLimitFloat"
            >

            <div v-else-if="sku.isImageField" class="sku-cover">
              <sortable-upload
                v-if="!sku.limit || sku.limit <= 1"
                v-model="sku.value"
                is-single
                :width="50"
                :height="50"
              />
              <div v-else @click="handleShowPicDialog(sku)">
                <img v-if="sku.value" :src="sku.value|firstImg" class="avatar">
                <i v-else class="el-icon-plus avatar-uploader-icon"/>
              </div>
            </div>
            <div v-else-if="sku.isDefaultSkuField" style="padding-left: 10px">
              <el-radio v-model="value.defaultSkuIndex" :label="skuPropIndex">{{ sku.title }}</el-radio>
            </div>
            <div v-else-if="sku.isOnlineOfflineField" class="sku-status">
              <div>{{ sku.value | onlineStatusFilter }}</div>
              <div v-if="sku.value===1" class="sku-status__btn" @click="sku.value=0">下架</div>
              <div v-else-if="sku.value===0" class="sku-status__btn" @click="sku.value=1">上架</div>
            </div>
            <span v-else>{{ sku.value }}</span>
          </td>
        </template>
      </tr>
      </tbody>
    </table>
    <div v-if="errMsg" class="errMsg-tip">{{ errMsg }}</div>
    <el-dialog :visible.sync="picDialogShowed" width="430px" append-to-body>
      <el-form>
        <el-form-item label="预览图">
          <sortable-upload v-model="goodsDescImg" :disabled="inputDisabled" :first-as-cover="true" :limit="picLimit"/>
        </el-form-item>
      </el-form>
      <div v-if="!inputDisabled" slot="footer" class="dialog-footer">
        <el-button type="default" @click="picDialogShowed=false">取消</el-button>
        <el-button type="primary" @click="handleSavePicList">确认</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import SkuUtil from '@/framework/components/sku/SkuUtil'
import SortableUpload from '@/framework/components/SortableUpload'
import SkuListEntity from '@/framework/components/sku/SkuListEntity'

export default {
  name: 'SkuListTable',
  components: { SortableUpload },
  filters: {
    onlineStatusFilter(status) {
      return status ? '已上架' : '已下架'
    },
    firstImg(v) {
      return v ? v.split(',')[0] : ''
    }
  },
  props: {
    disabled: {
      type: Boolean,
      default: false
    },
    value: {
      type: Object,
      default() {
        return new SkuListEntity()
      }
    }
  },
  inject: {
    skuEditor: {
      default: null
    }
  },
  data() {
    return {
      SkuUtil,
      errMsg: '',
      picDialogShowed: false,
      picLimit: 8,
      goodsDescImg: []
    }
  },
  computed: {
    inputDisabled() {
      return this.disabled || (this.skuEditor || {}).inputDisabled
    },
    isEdit() {
      return (this.skuEditor || {}).isEdit
    }
  },
  // watch: {
  //   'value.validSkuPropTypeList': {
  //     deep: true,
  //     handler() {
  //       this.value.calcSkuList()
  //     }
  //   }
  // },
  methods: {
    handleShowPicDialog(sku) {
      this.picDialogShowed = true
      this.goodsDescImg = sku.value ? sku.value.split(',') : []
      this.currentSku = sku
      this.picLimit = sku.limit || 8
    },
    handleSavePicList() {
      if (!this.goodsDescImg.length) {
        this.showError('请上传至少一张图片')
        return
      }
      this.currentSku.value = this.goodsDescImg.join(',')
      this.picDialogShowed = false
    }
  }
}
</script>

<style scoped lang="scss">

i.required:before {
  display: inline;
  padding-right: 4px;
  content: '*';
  color: red;
}

.avatar-uploader {
  width: 45px;
  height: 45px;
}

.avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}

.avatar-uploader .el-upload:hover {
  border-color: #409EFF;
}

.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 45px;
  height: 45px;
  line-height: 45px;
  text-align: center;
}

.avatar {
  width: 45px;
  height: 45px;
  display: block;
}

.sku-cover {
  display: flex;
  align-items: center;
  padding: 0 10px;
}

.stock-btns {
  display: flex;
  flex-direction: row;
  margin-right: 5px;
  font-size: 12px;
  position: absolute;
  left: 10px;
  top: 50%;
  transform: translateY(-50%);
  border: 1px solid #454a5f;
  border-radius: 2px;
}

.stock-btn {
  text-align: center;
  cursor: pointer;
  width: 21px;
  line-height: 20px;
  user-select: none;
}

.stock-btn--active {
  background-color: #454a5f;
  color: #fff;
}

input:disabled {
  background-color: #fff;
}

table {
  width: 100%;
  border-collapse: collapse;

  td, th {
    border: 1px solid #eee;
    font-size: 12px;
    text-align: left;
    padding: 15px 10px;
    line-height: 1;
  }

  td {
    padding: 0;

    span {
      padding: 20px 10px;
    }

    position: relative;
  }

  thead {
    background-color: #f7f7f7;
  }

  tbody {
    background-color: #fff;

    input {
      font-size: 12px;
      display: block;
      width: 100%;
      box-sizing: border-box;
      padding: 20px 10px;
      border: 1px solid transparent;

      &:hover {
        border-color: #4b97de;
      }

      &:focus {
        border-color: lightblue;
      }
    }
  }
}

</style>
